import pygame, sys
from SudokuSolver import SudokuGrid

class SudokuGridGui():
    def __init__(self,grid):
        self.done = False
        self.clock = pygame.time.Clock()
        self.cells = {}
        self.sudokuGrid = grid

        self.colors = {
                       "black":(0,0,0),
                       "white":(255,255,255),
                       "red":(255,0,0),
                       "green":(0,200,0),
                       "blue":(0,0,200),
                       "darkgrey":(128,128,128),
                       "lightgrey":(200,200,200)
                       }
        self.drawGrid()
        
        while self.done == False:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.done = True
                    
            self.updateGrid()
            self.clock.tick(30)
            pygame.display.flip()
        pygame.quit()
        
    
    def drawGrid(self):
        self.cellWidth, self.cellHeight = 24,24
        
        pygame.init()
        
        size = [self.cellWidth*27,self.cellHeight*27]
        
        self.screen = pygame.display.set_mode(size)
        pygame.display.set_caption("py-sudoku-solver-sdj")
        self.screen.fill(self.colors["white"])
        
         
        for row in range(28):
            color = self.colors["darkgrey"]
            width = 2
            if row%3 != 0:
                color = self.colors["lightgrey"]
                width = 1
            if row%9 == 0:
                color = self.colors["black"]
                width = 2
            pygame.draw.line(self.screen,color,(0,self.cellHeight*row),(self.cellWidth*27,self.cellHeight*row),width)
            
        for col in range(28):
            color = self.colors["darkgrey"]
            width = 2
            if col%3 != 0:
                color = self.colors["lightgrey"]
                width = 1
            if col%9 == 0:
                color = self.colors["black"]
                width = 2
            pygame.draw.line(self.screen,color,(self.cellWidth*col,0),(self.cellWidth*col,self.cellHeight*27),width)
            
    def setCellValue(self,cell,value):
        font = pygame.font.Font(None, 60)
        text = font.render(str(value),True,self.colors["blue"])
        tW = text.get_width()
        tH = text.get_height()
        tx = (cell.x-1)*self.cellWidth*3 + self.cellWidth*3/2 - tW/2
        ty = (cell.y-1)*self.cellWidth*3 + self.cellHeight*3/2 - tH/2 + 2
        self.screen.blit(text, (tx,ty))
        self.cells[(cell.x,cell.y)] = text
        
    def setCellossibilities(self,cell):
        font = pygame.font.Font(None, 22)
        for y in range(3):
            for x in range(1,4):
                value = x + y*3
                if value in cell.possibilities:
                    text = font.render(str(value),True,self.colors["red"])
                    tW = text.get_width()
                    tH = text.get_height()
                    tx = (cell.x-1)*self.cellWidth*3 + (x-1) * self.cellWidth + self.cellWidth/2 - tW/2
                    ty = (cell.y-1)*self.cellWidth*3 + y * self.cellHeight + self.cellHeight/2 - tH/2
                    self.screen.blit(text, (tx,ty))
                


    def updateGrid(self):
        for cell in self.sudokuGrid.getCellsList():
            if cell.value != 0:
                self.setCellValue(cell, cell.value)
            else:
                self.setCellossibilities(cell)
                    


